<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户名" prop="username">
        <el-input v-model="queryParams.username" placeholder="请输入商户名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="店铺名" prop="shopName">
        <el-input v-model="queryParams.shopName" placeholder="请输入店铺名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="approveStatus">
        <el-select
          v-model="queryParams.approveStatus"
          placeholder="全部状态"
          clearable
          style="width: 120px"
        >
          <el-option label="待审核" :value="1" />
          <el-option label="已通过" :value="2" />
          <el-option label="未通过" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['merchant:merchant:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['merchant:merchant:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['merchant:merchant:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['merchant:merchant:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="merchantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="商户名" align="center" prop="username" />

      <el-table-column label="头像" align="center" prop="avatar" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center" prop="mail" />
      <el-table-column label="电话号码" align="center" prop="tel" />
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺名" align="center" prop="shopName" />
      <el-table-column label="主营类" align="center" prop="mainManage" />
      <el-table-column label="身份证头像面" align="center" prop="idHeadPic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.idHeadPic" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="身份证国徽面" align="center" prop="idNationalEmblemPic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.idNationalEmblemPic" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="是否三证合一" align="center" prop="isThreeCertInOne" />
      <el-table-column label="营业执照" align="center" prop="businessLicensePic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.businessLicensePic" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="审批意见" align="center" prop="opinion" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
           v-if="scope.row.approveStatus === 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['systematic:merchant:opinionUpdate']"> <!-- 修正权限标识 -->
            审核
          </el-button>
          <!-- 审核状态为2时显示通过标签 -->
          <el-tag
            v-else-if="scope.row.approveStatus === 2"
            type="success"
            effect="dark">
            审核通过
          </el-tag>
          <!-- 审核状态为3时显示不通过标签 -->
          <el-tag
            v-else-if="scope.row.approveStatus === 3"
            type="danger"
            effect="dark">
            审核不通过
          </el-tag>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['merchant:merchant:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商户申请审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="商户名" prop="username">
          <el-input v-model="form.username" disabled />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-upload class="avatar-uploader" :action="uploadUrl" :show-file-list="false"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.avatar" :src="form.avatar" class="avatar" :width="200" :height="100" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="邮箱" prop="mail">
          <el-input v-model="form.mail" disabled />
        </el-form-item>
        <el-form-item label="电话号码" prop="tel">
          <el-input v-model="form.tel" disabled />
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker v-model="form.applyTime" type="datetime" placeholder="选择申请时间" disabled />
        </el-form-item>
        <el-form-item label="店铺名" prop="shopName">
          <el-input v-model="form.shopName" disabled />
        </el-form-item>
        <el-form-item label="主营类" prop="mainManage">
          <el-input v-model="form.mainManage" disabled />
        </el-form-item>
        <el-form-item label="身份证头像面" prop="idHeadPic">
          <el-upload class="avatar-uploader" :action="uploadUrl" :show-file-list="false"
            :before-upload="beforeIdHeadPicUpload">
            <img v-if="form.idHeadPic" :src="form.idHeadPic" class="avatar" :width="400" :height="200" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="身份证国徽面" prop="idNationalEmblemPic">
          <el-upload class="avatar-uploader" :action="uploadUrl" :show-file-list="false"
            :before-upload="beforeIdNationalEmblemPicUpload">
            <img v-if="form.idNationalEmblemPic" :src="form.idNationalEmblemPic" class="avatar" :width="400"
              :height="200" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="是否三证合一" prop="isThreeCertInOne">
          <el-input v-model="form.isThreeCertInOne" disabled />
        </el-form-item>
        <el-form-item label="营业执照" prop="businessLicensePic">
          <el-upload class="avatar-uploader" :action="uploadUrl" :show-file-list="false"
            :before-upload="beforeBusinessLicensePicUpload">
            <img v-if="form.businessLicensePic" :src="form.businessLicensePic" class="avatar" :width="400"
              :height="200" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">通 过</el-button>
        <el-button @click="cancel">不通过</el-button>
      </div>
    </el-dialog>

    <el-dialog title="提示" width="500px" :visible.sync="opinionDialogVisible" append-to-body>
      <el-form ref="opinionForm" :model="form" :rules="rules" label-width="80px">
        <span>输入审核意见</span>
        <el-input v-model="form.opinion" placeholder="请输入审核意见" type="textarea" :rows="4" />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="opinionDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitOpinion">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listMerchant,
    getMerchant,
    delMerchant,
    addMerchant,
    updateMerchant,
    opinionUpdate
  } from "@/api/systematic/merchant";

  export default {
    name: "Merchant",
    data() {
      return {
        // 遮罩层
        loading: true,
        opinionDialogVisible: false, // 控制意见对话框
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 商户申请审核表格数据
        merchantList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //只读
        isDisabled: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          username: null,
          shopName: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          username: [{
            required: true,
            message: "商户名不能为空",
            trigger: "blur"
          }],
          avatar: [{
            required: true,
            message: "头像不能为空",
            trigger: "blur"
          }],
          mail: [{
            required: true,
            message: "邮箱不能为空",
            trigger: "blur"
          }],
          tel: [{
            required: true,
            message: "电话号码不能为空",
            trigger: "blur"
          }],
          applyTime: [{
            required: true,
            message: "申请时间不能为空",
            trigger: "blur"
          }],
          shopName: [{
            required: true,
            message: "店铺名不能为空",
            trigger: "blur"
          }],
          mainManage: [{
            required: true,
            message: "主营类不能为空",
            trigger: "blur"
          }],
          idHeadPic: [{
            required: true,
            message: "身份证头像面不能为空",
            trigger: "blur"
          }],
          idNationalEmblemPic: [{
            required: true,
            message: "身份证国徽面不能为空",
            trigger: "blur"
          }],
          isThreeCertInOne: [{
            required: true,
            message: "是否三证合一不能为空",
            trigger: "blur"
          }],
          businessLicensePic: [{
            required: true,
            message: "营业执照不能为空",
            trigger: "blur"
          }],
          approveStatus: [{
            required: true,
            message: "审批状态不能为空",
            trigger: "change"
          }]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询商户申请审核列表 */
      getList() {
        this.loading = true;
        listMerchant(this.queryParams).then(response => {
          this.merchantList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.opinionDialogVisible = true; // 打开意见对话框
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          username: null,
          avatar: null,
          mail: null,
          tel: null,
          applyTime: null,
          shopName: null,
          mainManage: null,
          idHeadPic: null,
          idNationalEmblemPic: null,
          isThreeCertInOne: null,
          businessLicensePic: null,
          approveStatus: null,
          opinion: null
        };
        this.resetForm("form");
      },
      //审核意见提交
     submitOpinion() {
       this.$refs.opinionForm.validate(valid => {
           if (valid) {
             if (!this.form.opinion) { // 新增校验
               this.$modal.msgError("请填写审核意见");
               return;
             }
             this.form.approveStatus = 3;
             opinionUpdate(this.form).then(response => {
               this.$modal.msgSuccess("审核不通过已提交");
               this.opinionDialogVisible = false;
               this.open = false;
               this.getList();
             });
           }
         });
     },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加商户申请审核";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getMerchant(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改商户申请审核";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            // 设置审核状态为2（通过）
            this.form.opinion = "";
            this.form.approveStatus = 2;

            opinionUpdate(this.form).then(response => {
              this.$modal.msgSuccess("审核通过");
              this.open = false;
              this.getList(); // 刷新列表
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除商户申请审核编号为"' + ids + '"的数据项？').then(function() {
          return delMerchant(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('merchant/merchant/export', {
          ...this.queryParams
        }, `merchant_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
